//
//  XSMainViewController.swift
//  swiftWeibo
//
//  Created by 马雪山 on 2017/7/18.
//  Copyright © 2017年 xueshanma. All rights reserved.
//

import UIKit

class XSMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()


        setupChildController()
        
        setupComposeButton()
        
        
        
    }
    //支持屏幕方向
    //代码控制
    /*
     portrait  竖屏(肖像)
     landscape 横屏(风景画)
     
     --使用代码控制设备的方向, 可以在需要横屏的时候,单独处理
     --设置支持的方向后,当前的控制器及子控制器,都会遵守这个方向
     -- 如果播放视频,通常是通过 modal 展现的
     
     */
    //你可以重写该属性的set和get方法, 来设置控制器支持的旋转方向, 例如:
    private var _orientations = UIInterfaceOrientationMask.portrait
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        set{
            _orientations = newValue;
        }
        get{
            return _orientations;
        }
    }
    
    
    
    //MARK: 监听方法
    //点击撰写微博按钮
    //fileprivate 代表文件内私有
    //private 当前类私有
    //@objc 允许这个函数在'运行时"通过OC的消息机制被调用
    @objc fileprivate func composeStatus() {
        print("写微博")
        //测试方向旋转
        
        let vc = UIViewController()
        vc.view.backgroundColor = UIColor.red
        let nav = UINavigationController(rootViewController: vc)
        
        present(nav , animated: true, completion: nil)
        
        
    }
    
    //MARK: 私有控件
    //tabBar中间按钮
     lazy var composeButton: UIButton = {()->UIButton in
        let btn = UIButton.init()
        btn.setImage(UIImage.init(named: "tabBar_publish_icon"), for: .normal)
        btn.setImage(UIImage.init(named: "tabBar_publish_click_icon"), for: .selected)
        
        return btn
    }()
    


}

//extension 类似OC中的分类, 在swift中 还可以用来切分代码块
//可以把功能相似的函数, 放在一个extension 中, 便于代码维护
//注意:和OC中的分类一样,extension中不能定义属性

//MARK: - 设置界面
extension XSMainViewController{
    
    func setupComposeButton()  {
        tabBar.addSubview(composeButton)
        //设置按钮的位置
        let count = CGFloat((viewControllers?.count)!)
        let w = tabBar.bounds.width/count
        composeButton.frame = CGRect(x: 2*w, y: 0, width: w, height: tabBar.bounds.size.height)
        //按钮的犯法
        composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
        
    }
    
    //设置所有子控制器
     func setupChildController(){
        let array = [
            //可以换成网络图片, 可以动态切换
            ["clsName": "XSHomeViewController", "title": "首页","imageName":"tabBar_essence"],
            ["clsName": "XSMessageViewController", "title": "消息","imageName":"tabBar_friendTrends"],
            ["clsName": "UIViewController"],
            ["clsName": "XSDiscoverViewController", "title": "发现","imageName":"tabBar_new"],
            ["clsName": "XSProfileViewController", "title": "我","imageName":"tabBar_me"],

        ]
        
        var arrayM = [UIViewController]()
        for dict in array {
            
            arrayM.append(controller(dict: dict))
        }
        
        viewControllers = arrayM
    }
    //使用字典,创建一个子控制器
    //dict:信息字典[className,title,imageName
    private func controller(dict: [String: String])-> UIViewController{
        //1.字典内容
        guard let clsName = dict["clsName"],
        let title = dict["title"],
        let image = dict["imageName"],
        let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
        else {
            
            return UIViewController()
        }
        //2.创建视图控制器
        let vc = cls.init()
        //tabBar_me_click_icon
        vc.title = title
        vc.tabBarItem.image = UIImage(named: image + "_icon")
        vc.tabBarItem.selectedImage = UIImage(named: image + "_click_icon")?.withRenderingMode(.alwaysOriginal)
        
        //3.设置tabBar标题字体(高亮时的,状态是highlighted)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.red], for: .highlighted)
        //字体大小(系统默认是12 号字,状态是normal)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 13)], for:.normal)
        
        //实例化导航控制器,会调用push方法将rootVC 压栈
        let nav = XSNavigationController(rootViewController: vc)
        
        return nav
    }
    
}














